# from .ZedCamera.main import zedcamera
import numpy as np
import os
import sys

from torch import double
from data.base_dataset import BaseDataset
sys.path.append(os.path.abspath("."))
import cv2
import pyzed.sl as sl

camera_settings = sl.VIDEO_SETTINGS.BRIGHTNESS
str_camera_settings = "BRIGHTNESS"
step_camera_settings = 1


def print_camera_information(cam):
    print("Resolution: {0}, {1}.".format(round(cam.get_camera_information().camera_resolution.width, 2), cam.get_camera_information().camera_resolution.height))
    print("Camera FPS: {0}.".format(cam.get_camera_information().camera_fps))
    print("Firmware: {0}.".format(cam.get_camera_information().camera_firmware_version))
    print("Serial number: {0}.\n".format(cam.get_camera_information().serial_number))


def print_help():
    print("Help for camera setting controls")
    print("  Increase camera settings value:     +")
    print("  Decrease camera settings value:     -")
    print("  Switch camera settings:             s")
    print("  Reset all parameters:               r")
    print("  Record a video:                     z")
    print("  Quit:                               q\n")

    
class ZedRGBDataset(BaseDataset):
    def modify_commandline_options(parser, flagdict=None):
        """Add new dataset-specific options, and rewrite default values for existing options.

        Parameters:
            parser          -- original option parser
            is_train (bool) -- whether training phase or test phase. You can use this flag to add training-specific or test-specific options.

        Returns:
            the modified parser.
        """
        parser.add_argument('--ml_name_list', type=str, default='', help="The list of ML, separate by ','")
        parser.add_argument('--ml_checkpoints_dir', type=str, default='./datasets/checkpoints', help='The dir of machine learning model dirs')
        


        return parser
    def change_options(self,opt):
        opt.yreverse=-1
        return opt
    def __init__(self,opt):
                #  init=defaultinit(),
                #  runtime_parameters=defaultruntimeparameters(),
                #  res=defaultres(),
                #  tr_np=defaulttr_np()):
        print("Running...")
        init = sl.InitParameters()
        self.cam = sl.Camera()
        if not self.cam.is_opened():
            print("Opening ZED Camera...")
        status = self.cam.open(init)
        if status != sl.ERROR_CODE.SUCCESS:
            print(repr(status))
            exit()

        self.runtime = sl.RuntimeParameters()
        self.mat = sl.Mat()

        print_camera_information(self.cam)
        print_help()
    
    def __len__(self):
        return 0
    def __getitem__(self,idx):
        err = self.cam.grab(self.runtime)
        if err == sl.ERROR_CODE.SUCCESS:
            self.cam.retrieve_image(self.mat, sl.VIEW.LEFT)
            rgb = self.mat.get_data()
            return rgb[:,:,:3]